<?php
include_once '../Login/databaseconnection.php';
$Competition_Name=$_POST['Competition_Name'];
$New_Competition=$_POST['New_Competition'];
if($New_Competition!=""&&$Competition_Name!="Select One")
{
$_SESSION['error']="Please select the competition or type new!";
}
else
{
if($New_Competition!="")
{
$maxid=mysql_query("SELECT max(	Competition_Id) as maxId FROM competitions");
while($rowid=mysql_fetch_array($maxid))
{
$m_Id=$rowid['maxId'];
 $inum=substr("$m_Id",1);
$inn=($inum+1);
$Competition_Id='C';
if(strlen($inn)<strlen($inum))
{
for($i=0;$i<(strlen($inum)-strlen($inn));$i++)
{
$Competition_Id=$Competition_Id.'0';
}
$Competition_Id=$Competition_Id.$inn;
}
else
{
$Competition_Id=$Competition_Id.$inn;
}
}
}
date_default_timezone_set("Europe/Paris");
$t_d=date("Y-m-d");
$sql_nc=mysql_query("INSERT INTO competitions(Competition_Id,Competition_Name,Last_Updated_Date) VALUES('$Competition_Id','$New_Competition','$t_d')");
if($sql_nc)
  {
$Competition_Name=$New_Competition;
  }

$resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Name='$Competition_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Id=$rownet['Competition_Id'];
}
$No_Of_Students=$_POST['No_Of_Students'];
$No_Of_Teachers=$_POST['No_Of_Teachers'];
$Result=$_POST['Result'];
$Date=$_POST['Date'];
$Scope=$_POST['Scope'];
$Winner=$_POST['Winner'];
  
  $SchName=$_POST['SchName'];
   $name=explode(",",$SchName);
    $nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
   
if(($Competition_Name!="Select One"||$New_Competition!="")&&$SchName!=""&&$No_Of_Students!=""&&$No_Of_Teachers!=""&&$Result!=""&&$Date!=""&&$Scope!="")
	{
	if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];  
   if($School_Id==$Own_School_Id)
	{
	if($Result=="Win"&&$Winner=="")
	{
	$_SESSION['errornotallo']="Please enter the winner's name!";
	}
	else if(($Result=="Win"&&$Winner!="")||$Result=="Loss")
	{
	$sql=mysql_query("INSERT INTO competition(Competition_Id,School_Id,No_Of_Students,No_Of_Teachers,Result,Date,Scope,Winner,Last_Updated_Date)values('$Competition_Id','$School_Id','$No_Of_Students','$No_Of_Teachers','$Result','$Date','$Scope','$Winner','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Competition_Id']=$Competition_Id;
$_SESSION['School_Id']=$School_Id;
$_SESSION['Date']=$Date;
  }

  if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
	}
	}
	else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	}
	else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	}
	else if($_SESSION['Access_Type']=="Administrator")
{
if($Result=="Win"&&$Winner=="")
	{
	$_SESSION['errornotallo']="Please enter the winner's name!";
	}
	else if($Result=="Win"&&$Winner!="")
	{
$sql=mysql_query("INSERT INTO competition(Competition_Id,School_Id,No_Of_Students,No_Of_Teachers,Result,Date,Scope,Winner,Last_Updated_Date)values('$Competition_Id','$School_Id','$No_Of_Students','$No_Of_Teachers','$Result','$Date','$Scope','$Winner','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Competition_Id']=$Competition_Id;
$_SESSION['School_Id']=$School_Id;
$_SESSION['Date']=$Date;
  }

  if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
  }
  else if($Result=="Loss")
{
$sql=mysql_query("INSERT INTO competition(Competition_Id,School_Id,No_Of_Students,No_Of_Teachers,Result,Date,Scope,Winner,Last_Updated_Date)values('$Competition_Id','$School_Id','$No_Of_Students','$No_Of_Teachers','$Result','$Date','$Scope','$Winner','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Competition_Id']=$Competition_Id;
$_SESSION['School_Id']=$School_Id;
$_SESSION['Date']=$Date;
  }

  if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
}
  }
  else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
  }
  if(($Competition_Name=="Select One"&&$New_Competition=="")||$SchName==""||$No_Of_Students==""||$No_Of_Teachers==""||$Result==""||$Date==""||$Scope=="")
	{
	$_SESSION['errorin']="Indexed details are required,Please fill all!";
  }
  }
?>

